package cn.edu.sdjzu.xg.bysj.dao;

import cn.edu.sdjzu.xg.bysj.domain.Department;
import cn.edu.sdjzu.xg.bysj.domain.StudentClass;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentClassDao {
    //创建StudentClassDao对象
    private static StudentClassDao studentDao = new StudentClassDao();
    //私有构造器
    private StudentClassDao(){}
    //获取实例
    public static StudentClassDao getInstance(){
        return studentDao;
    }
    //查找
    public StudentClass find(Connection connection , int id) throws SQLException {
        //预编译语句
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM studentClass where id=?");
        preparedStatement.setInt(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        StudentClass studentClass = null;
        if (resultSet.next()){
            int class_id = resultSet.getInt("id");
            String description = resultSet.getString("description");
            String no = resultSet.getString("no");
            String remarks = resultSet.getString("remarks");
            Department department = DepartmentDao.getInstance().find(connection,resultSet.getInt("department_id"));
            studentClass = new StudentClass(class_id,description,no,remarks,department);
        }
        JdbcHelper.close(resultSet,preparedStatement);
        return studentClass;

    }
}
